package com.cosmoplat.xikai.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cosmoplat.xikai.business.domain.BaseProductInfo;
import com.cosmoplat.xikai.business.domain.DeviceProductLog;
import com.cosmoplat.xikai.business.domain.NumberOfProductionRecords;
import com.cosmoplat.xikai.business.domain.dto.quality.QualityQueryDTO;
import com.cosmoplat.xikai.business.domain.vo.ProcessingDataVo;
import com.cosmoplat.xikai.business.domain.vo.ProductQuality;
import com.cosmoplat.xikai.business.service.BaseProductInfoService;
import com.cosmoplat.xikai.business.mapper.BaseProductInfoMapper;
import com.cosmoplat.xikai.business.service.DeviceProductLogService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 22047582
 * @description 针对表【base_product_info】的数据库操作Service实现
 * @createDate 2025-01-07 13:51:30
 */
@Service
public class BaseProductInfoServiceImpl extends ServiceImpl<BaseProductInfoMapper, BaseProductInfo> implements BaseProductInfoService {

    @Autowired
    private DeviceProductLogService deviceProductLogService;

    @Override
    public List<ProcessingDataVo> queryProcessingDataForPage(QualityQueryDTO queryDTO) {
        List<ProcessingDataVo> processingDataVos = this.getBaseMapper().queryProcessingDataForPage(queryDTO);
        if (CollectionUtils.isEmpty(processingDataVos)) {
            return Collections.emptyList();
        }
        List<Long> ids = processingDataVos.stream().map(BaseProductInfo::getId).collect(Collectors.toList());
        List<DeviceProductLog> deviceProductLogs = deviceProductLogService.getListByProductIdsForProcessing(ids);
        if (CollectionUtils.isEmpty(deviceProductLogs)) {
            return processingDataVos;
        }
        // 处理最大扭矩值
        Map<Long, Double> productLogMap = deviceProductLogs.stream().collect(Collectors.toMap(DeviceProductLog::getProductId, DeviceProductLog::getMeasurTorque));
        processingDataVos.forEach(processingDataVo -> {
            if (productLogMap.containsKey(processingDataVo.getId())) {
                processingDataVo.setMeasurTorque(productLogMap.get(processingDataVo.getId()));
            }
        });
        return processingDataVos;
    }

    @Override
    public List<ProductQuality> queryProductQuality(QualityQueryDTO queryDTO) {
        return this.getBaseMapper().queryProductQuality(queryDTO);
    }

    @Override
    public List<NumberOfProductionRecords> queryNumberOfProductionRecords() {
        return this.getBaseMapper().queryNumberOfProductionRecords();
    }

    @Override
    public ArrayList<String> getProductNameList() {
        return this.getBaseMapper().getProductNameList();
    }
}




